<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\AboutUsModel;
use app\model\ActiveParticipantsModel;
use app\model\ActivitiesModel;
use app\model\BooksModel;
use app\model\RedisModel;
use app\model\UserModel;
use think\App;

class Activities extends Base {

    protected $middleware = [Auth::class];
    private $model;

    private  $activeParticipantsModel;

    private $userModel;

    private $req;

    private $aid;

    private $page;

    private $limit;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->model = new ActivitiesModel();

        $this->activeParticipantsModel = new ActiveParticipantsModel();

        $this->userModel = new UserModel();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 20;

        $this->aid = $this->isLogin();
    }



    public function getList(){

        $where =[];
        $where[] = ["is_delete","=",0];

        if(isset($this->req["active_status"]) && $this->req["active_status"] != "" ){
            $where[] = ["active_status","=",$this->req["active_status"]];
        }

        $list = $this->model->getList($where,$this->page,$this->limit,["created_at"=>"DESC"]);

        $total = $this->model->getTotal($where);

        return $this->succData($total,$list);

    }

    /*
     * 获取店铺列表
     * */
    public function getActiveInfo(){

        if(!isset($this->req['active_id']) || empty($this->req['active_id'])){
            return $this->fail("活动Id不能为空");
        }

        $info = $this->model->getInfo($this->req['active_id']);

        return $this->succ($info);
    }


    public function getActiveSignUpList(){

        if(!isset($this->req['active_id']) || empty($this->req['active_id'])){
            return $this->fail("活动Id不能为空");
        }

        $where = [];
        $where["active_id"] = $this->req['active_id'];
        $sign_up_list = $this->activeParticipantsModel->getList($where,$this->page,$this->limit,"created_at","DESC");
        if($sign_up_list){
            foreach ($sign_up_list as &$u){
                $user = $this->userModel->getUserInfoByUid($u["uid"]);
                $u['avatar'] = $user ? $user["avatar"] : "";
            }
        }
        $total = $this->activeParticipantsModel->getTotal($where);

        return $this->succData($total,$sign_up_list);
    }



    /*
    * 新增/删除/修改
    * */
    public function save(){

        if(!isset($this->req['active_name']) || empty($this->req['active_name'])){
            return $this->fail("活动名称不能为空");
        }

        if(!isset($this->req['active_introduction']) || empty($this->req['active_introduction'])){
            return $this->fail("活动介绍不能为空");
        }

        if(!isset($this->req['active_address']) || empty($this->req['active_address'])){
            return $this->fail("活动地点不能为空");
        }

        if(!isset($this->req['start_time']) || empty($this->req['start_time'])){
            return $this->fail("活动开始时间不能为空");
        }

        if(!isset($this->req['end_time']) || empty($this->req['end_time'])){
            return $this->fail("活动结束时间不能为空");
        }

        if(!isset($this->req['stop_time']) || empty($this->req['stop_time'])){
            return $this->fail("截止报名时间不能为空");
        }

        if(!isset($this->req['organizer_wx']) || empty($this->req['organizer_wx'])){
            return $this->fail("联系人微信不能为空");
        }

        if(!isset($this->req['active_image_url']) || empty($this->req['active_image_url'])){
            return $this->fail("封面图不能为空");
        }

        if(!isset($this->req['active_details']) || empty($this->req['active_details'])){
            return $this->fail("详情不能为空");
        }


        if(isset($this->req["active_id"]) && !empty($this->req["active_id"])){
            $old = $this->model->where(["active_id"=>$this->req["active_id"]])->find();

            //扩容处理
            if($old["active_status"] == 2 &&
                strtotime($this->req['stop_time']) > time() &&
                $this->req['active_max_num'] != 0 &&
                $this->req['active_max_num'] > $old["active_current_num"] &&
                ($this->req['stop_time']!= $old["stop_time"] || $this->req['active_max_num'] != $old["active_max_num"])){
                $this->req["active_status"] = 1;
            }else  if($old["active_status"] == 2 &&
                strtotime($this->req['stop_time']) > time() &&
                $this->req['active_max_num'] == 0 &&
                $this->req['stop_time']!= $old["end_time"] ){
                $this->req["active_status"] = 1;
            }


            $res = $this->model->updateOne(["active_id"=>$this->req["active_id"]],[
                "active_name"=>$this->req['active_name'],
                "active_introduction"=>$this->req['active_introduction'],
                "active_details"=>$this->req['active_details'],
                "active_image_url"=>$this->req['active_image_url'],
                "active_max_num"=>isset($this->req['active_max_num']) && empty($this->req['active_max_num']) ? 0 :$this->req['active_max_num'],
                "stop_time"=>$this->req['stop_time'],
                "start_time"=>$this->req['start_time'],
                "end_time"=>$this->req['end_time'],
                "active_address"=>$this->req['active_address'],
                "active_price"=>isset($this->req['active_price']) && empty($this->req['active_price']) ? 0 :$this->req['active_price'],
                "organizer_wx"=>$this->req['organizer_wx'],
                "active_status"=>isset($this->req['active_status']) && $this->req['active_status'] == "" ? 0 :$this->req['active_status'],
                "updated_at"=>date("Y-m-d H:i:s")
            ]);
        }else{
            $res = $this->model->addOne([
                "active_name"=>$this->req['active_name'],
                "active_introduction"=>$this->req['active_introduction'],
                "active_details"=>$this->req['active_details'],
                "active_image_url"=>$this->req['active_image_url'],
                "active_max_num"=>$this->req['active_max_num'],
                "stop_time"=>$this->req['stop_time'],
                "start_time"=>$this->req['start_time'],
                "end_time"=>$this->req['end_time'],
                "active_address"=>$this->req['active_address'],
                "active_price"=>empty($this->req['active_price']) ? 0 :$this->req['active_price'],
                "organizer_wx"=>$this->req['organizer_wx'],
                "active_status"=>isset($this->req['active_status']) && $this->req['active_status'] == "" ? 0 :$this->req['active_status'],
            ]);
        }

        if($res){
            $this->model->delCache($this->req["active_id"]);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }





    /*
* 新增/删除/修改
* */
    public function updateStatus()
    {
        if(!isset($this->req['active_id']) || empty($this->req['active_id'])){
            return $this->fail("请选择要处理的活动");
        }

        if(!isset($this->req['active_status']) || !in_array($this->req['active_status'],[1,-1,2])){
            return $this->fail("请选择操作内容");
        }
        $res = $this->model->updateOne(["active_id"=>$this->req['active_id']],[
            "active_status"=>$this->req['active_status'],
        ]);

        if($res){
            $this->model->delCache($this->req["active_id"]);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }


    /*
     * 删除
     * */
    public function del()
    {
        if(!isset($this->req['active_id']) || empty($this->req['active_id'])){
            return $this->fail("请选择要处理的活动");
        }

        $res = $this->model->updateOne(["active_id"=>$this->req['active_id']],[
            "is_delete"=>1,
        ]);

        if($res){
            $this->model->delCache($this->req["active_id"]);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }


}
